<?php
namespace AuctionCore\CarDealer;
use AuctionCore\AuctionOrder\AuOrderList;
use AuctionCore\CarResource\CarModel;
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//


/**
* "出价的车"列表
* @author       爱民
*/
class BidCarList extends AuOrderList
{

    private $dealer_id = null;

    private $base_sql = null;
    private $count_sql = null;

    public function __construct($dealer_id) {
        $this->dealer_id = $dealer_id;

        $query_sql = "SELECT a.order_id,a.order_no,a.bidding_end_time,a.bidding_best_dealer_id,a.deliver_type,a.trade_time,
            a.bid_best_price,a.bidding_best_price,a.`status`,b.car_id,b.car_no,b.plate_prefix,b.plate_suffix,b.car_color,b.car_status,
            b.model_id,pay_type,b.first_reg_date,b.brand_id,b.series_id,a.reserve_price,ada.dealer_id as success_dealer_id,b.transfer_limit";
        $sql =" FROM au_order a LEFT JOIN au_cars b  ON a.car_id = b.car_id
                LEFT JOIN au_dealer_auctioned ada ON a.order_id=ada.order_id AND ada.status=0
                 WHERE (a.status >= 5 AND a.status !=301 )  AND
                (EXISTS(SELECT 1 FROM au_bid_log WHERE order_id=a.order_id AND dealer_id={$this->dealer_id} ) 
                OR 
                EXISTS(SELECT 1 FROM au_bidding_log WHERE order_id=a.order_id AND dealer_id={$this->dealer_id}))";

        $this->base_sql = $query_sql.$sql ;
        $this->count_sql = "select count(*) count ".$sql ;
    }


    /**
    * 获取列表结果
    * @param    int $ev_page_num    每页条数
    * @param    int $page    页码
    * @return   Object
    */
    public function get($ev_page_num, $page){

        $dealer_id = $this->dealer_id;
        $Model = new \Think\Model();
        
        //拍单列表信息
        $sql = $this->base_sql;
        $sql .= $this->condition;
        $skip = ($page<2?0:$ev_page_num*($page-1)) ;
        $sql .= " order by a.bidding_end_time desc limit {$skip},{$ev_page_num}" ;
        $sql .= ";";
        $list = $Model->query($sql);
        $cm = new CarModel();
        foreach ($list as $key => $row) {
            //车型信息
            $brand = $cm->get_model_by_id($row['brand_id'],'brand');
            $series = $cm->get_model_by_id($row['series_id'],'series');
            $model = $cm->get_model_by_id($row['model_id']);
            $list[$key]['model_info'] = $brand['name'].' '.$series['name'].' '.$model['name'];
            //最后投标价
            $r = $Model->query("SELECT price FROM au_bid_log WHERE order_id={$row['order_id']} AND dealer_id={$this->dealer_id} order by createtime desc limit 1");
            $list[$key]['my_bid_max_price'] = $r[0]['price'];
            //最高竞标价
            $r = $Model->query("SELECT MAX(after_price) maxprice FROM au_bidding_log WHERE order_id={$row['order_id']} AND dealer_id={$this->dealer_id}");
            $list[$key]['my_bidding_max_price'] = $r[0]['maxprice'];
            //成交价
            $list[$key]['success_price'] = $row['bid_best_price']>=$row['bidding_best_price'] ? $row['bid_best_price'] : $row['bidding_best_price'];
            //是否竞得
            $d = M("dealer_auctioned")->where("`dealer_id`={$dealer_id} AND `order_id`={$row['order_id']} AND `status`=0")->getField("dealer_id");
            if($d==$dealer_id) $list[$key]['is_get'] = true;
        }
        return $list;
    }

    public function count(){
        $Model = new \Think\Model();
        $sql = $this->count_sql;
        $sql .= " {$this->condition}";
        $sql .= ";";
        $r = $Model->query($sql);
        return $r[0]['count'];
    }

}

?>